﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using System.Data;
using Common;
using WebComputers.Class;

namespace WebComputers.AdministratorHA
{
    public partial class frmDonViTinh : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["DVTSave"] = Server.UrlEncode(System.DateTime.Now.ToString());
                //grvDonViTinh.GridLines = GridLines.None;
                //grvDonViTinh.CellSpacing = -1;
                FillDataComboBox();
                FillData();
            }
            else
            {
                if (grvDonViTinh.Rows.Count > 0)
                {
                    grvDonViTinh.HeaderRow.TableSection = TableRowSection.TableHeader;
                }
            }

        }
        //Chinh lai file proc 
        protected DataTable BindGridView()
        {
            try
            {
                DataTable dt = new DataTable();
                if (DAHelper.CheckNumber(txtPageIndex.Text))
                {
                    int pageIndex = Convert.ToInt32(txtPageIndex.Text);
                    if (pageIndex <= 0) return dt;
                    int pageSize = Convert.ToInt32(ddlPageSize.SelectedItem.Value);
                    int totalPages = Convert.ToInt32(txtTotalPages.Text);
                    if (totalPages > 0)
                    {
                        if ((pageIndex -1) * pageSize >= totalPages * Convert.ToInt32(hdfPageSize.Value))
                        {
                            //pageIndex = 1;
                            pageIndex = (int) Math.Ceiling((double)(totalPages * Convert.ToInt32(hdfPageSize.Value)) / pageSize);
                            txtPageIndex.Text = pageIndex.ToString();
                        }
                    }
                    dt = DonViTinhDAO.SelectAllForPaging(pageIndex, pageSize, ref totalPages);
                    txtTotalPages.Text = DAHelper.CalculateTotalPages(totalPages, pageSize).ToString();
                    hdfPageSize.Value = pageSize.ToString();
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        protected void FillDataComboBox()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PageSize", typeof(System.Int32));
            for (int i = 10; i <= 40; i += 10)
            {
                dt.Rows.Add(i);
            }
            ddlPageSize.DataSource = dt;
            ddlPageSize.DataTextField = "PageSize";
            ddlPageSize.DataValueField = "PageSize";
            ddlPageSize.DataBind();
            hdfPageSize.Value = ddlPageSize.SelectedItem.Value;
        }

        protected void FillData()
        {
            try
            {
                DataTable dt = BindGridView();
                if (dt.Rows.Count > 0)
                {
                    dt.Columns.Add("STT");
                    int i = (Convert.ToInt32(txtPageIndex.Text) - 1) * Convert.ToInt32(ddlPageSize.SelectedItem.Value) + 1;
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        dt.Rows[j]["STT"] = i;
                        i++;
                    }
                }


                grvDonViTinh.DataSource = dt;
                grvDonViTinh.DataBind();
                if (grvDonViTinh.Rows.Count > 0)
                {
                    //This replaces <td> with <th> and adds the scope attribute
                    //grvDonViTinh.UseAccessibleHeader = true;

                    //This will add the <thead> and <tbody> elements
                    grvDonViTinh.HeaderRow.TableSection = TableRowSection.TableHeader;

                    //This adds the <tfoot> element. 
                    //Remove if you don't have a footer row
                    //grvDonViTinh.FooterRow.TableSection = TableRowSection.TableFooter;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void grvDonViTinh_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(grvDonViTinh.DataKeys[e.RowIndex].Value);
                DonViTinhDAO.Delete(id);
                FillData();

            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        protected void grvDonViTinh_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(grvDonViTinh.DataKeys[e.NewSelectedIndex].Value);
                DonViTinh dvt = DonViTinhDAO.SelectById(id);
                hdfID.Value = dvt.iDonViTinh_ID.ToString();
                txtTenDonViTinh.Text = dvt.sTenDonViTinh;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["DVTSave"].ToString() == ViewState["DVTSave"].ToString())
                {
                    DonViTinh dvt = new DonViTinh();
                    dvt.sTenDonViTinh = txtTenDonViTinh.Text;
                    if (hdfID.Value == "0")
                    {
                        DonViTinhDAO.Insert(dvt);
                    }
                    else
                    {
                        dvt.iDonViTinh_ID = Convert.ToInt32(hdfID.Value);
                        DonViTinhDAO.Update(dvt);
                    }
                    Session["DVTSave"] = Server.UrlEncode(System.DateTime.Now.ToString());
                }                
                ResetField();
                FillData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void ResetField()
        {
            hdfID.Value = "-1";
            txtTenDonViTinh.Text = string.Empty;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "First":
                    txtPageIndex.Text = "1";
                    break;
                case "Prev":
                    if (DAHelper.CheckNumber(txtPageIndex.Text))
                    {
                        if (Convert.ToInt32(txtPageIndex.Text) > 1)
                            txtPageIndex.Text = (Convert.ToInt32(txtPageIndex.Text) - 1).ToString();
                        else
                            return;
                    }
                    break;

                case "Next":
                    if (DAHelper.CheckNumber(txtPageIndex.Text))
                    {
                        if (Convert.ToInt32(txtPageIndex.Text) < Convert.ToInt32(txtTotalPages.Text))
                            txtPageIndex.Text = (Convert.ToInt32(txtPageIndex.Text) + 1).ToString();
                        else
                            return;
                    }
                    break;

                case "Last":
                    txtPageIndex.Text = txtTotalPages.Text;
                    break;
            }

            FillData();
        }

        protected void txtPageIndex_TextChanged(object sender, EventArgs e)
        {
            try
            {
                FillData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                FillData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            ViewState["DVTSave"] = Session["DVTSave"];
        }
    }
}